package com.xing.config;

import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/4/14 15:50
 */
@Configuration
@Slf4j
public class SocketIOConfig {

    @Bean
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration configuration = new com.corundumstudio.socketio.Configuration();
        configuration.setPort(9092);
        SocketIOServer server = new SocketIOServer(configuration);
        server.addConnectListener(client -> {
            log.info("客户端：{}已连接：", client.getSessionId());
        });
        server.addDisconnectListener(client -> {
            log.info("客户端：{}已断开连接：", client.getSessionId());
        });
        server.addEventListener("msgEvent", String.class, (client, s, ackRequest) -> {
            log.info("客户端: {}发送消息-{}", client.getSessionId(), s);
            server.getBroadcastOperations().sendEvent("msgEvent", s);
        });
        return server;
    }
}
